/**
 * 我的小视频详情
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/my/buyvideoBloc.dart';
import 'package:qgchat/bloc/my/lovevideoBloc.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/buyvideoModel.dart';
import 'package:qgchat/http/models/lovevideModel.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/widgets/noData.dart';

class VideoInfo extends StatefulWidget {
  VideoInfo({Key key}) : super(key: key);

  @override
  VideoInfoState createState() => VideoInfoState();
}

class VideoInfoState extends State<VideoInfo>
    with SingleTickerProviderStateMixin {
  List inviteList = [0, 1, 3];

  // 顶部tabbar
  final tabs = ["喜欢", "购买"];

  // 跳转发布视频
  void release() {
    goPage(context, '/releasevideo');
  }

  // 构建顶部tabbar
  Widget getTabBar() {
    return TabBar(
      indicatorColor: col("d35edd"),
      labelStyle: TextStyle(fontWeight: FontWeight.bold),
      unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
      indicatorSize: TabBarIndicatorSize.label,
      indicatorWeight: px(6),
      tabs: tabs.map((t) {
        return Tab(
          child: Text(t),
        );
      }).toList(),
    );
  }

  // 构建喜欢tabview里面的内容
  Widget getTabview(context, bloc) {
    return StreamBuilder(
      stream: bloc.callStream,
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ) {
        List<LovevideoModel> list = bloc.loveonemore;
        if (snapshot.hasData || list.isNotEmpty) {
          if (list.isNotEmpty) {
            return Container(
              color: Colors.white,
              child: EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  bloc.onRefresh();
                },
                onLoad: () async {
                  bloc.getMake();
                },
                child: GridView.count(
                  //水平子Widget之间间距
                  crossAxisSpacing: px(22),
                  //垂直子Widget之间间距
                  mainAxisSpacing: px(22),
                  //GridView内边距
                  padding:
                      EdgeInsets.only(left: px(30), right: px(30), top: px(20)),
                  //一行的Widget数量
                  crossAxisCount: 2,
                  childAspectRatio: 0.75,
                  //子Widget列表
                  children: list.map<Widget>((f) {
                    return getItemContainer(context, f);
                  }).toList(),
                ),
              ),
            );
          } else {
            return NoData('我还没找到心动的视频');
          }
        } else {
          return NoData('我还没找到心动的视频');
        }
      },
    );
  }

  // 构建购买tabview里面的内容
  Widget getBuyTabview(context, bloc) {
    return StreamBuilder(
      stream: bloc.callStream,
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ) {
        List<BuyvideoModel> list = bloc.buyonmore;
        if (snapshot.hasData || list.isNotEmpty) {
          if (list.isNotEmpty) {
            return Container(
              color: Colors.white,
              child: EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  bloc.onRefresh();
                },
                onLoad: () async {
                  bloc.getMake();
                },
                child: GridView.count(
                  //水平子Widget之间间距
                  crossAxisSpacing: px(22),
                  //垂直子Widget之间间距
                  mainAxisSpacing: px(22),
                  //GridView内边距
                  padding:
                      EdgeInsets.only(left: px(30), right: px(30), top: px(20)),
                  //一行的Widget数量
                  crossAxisCount: 2,
                  childAspectRatio: 0.75,
                  //子Widget列表
                  children: list.map<Widget>((f) {
                    return getItemContainer(context, f);
                  }).toList(),
                ),
              ),
            );
          } else {
            return NoData('我还没购买视频');
          }
        } else {
          return NoData('我还没购买视频');
        }
      },
    );
  }

  // 单个小视频
  Widget getItemContainer(context, item) {
    return GestureDetector(
      onTapUp: (val) {
        goPage(context, '/innervideo', param: {'id': item.id,'image':item.thumbnail});
      },
      child: Container(
        width: px(334),
        height: px(445),
        decoration: ShapeDecoration(
          image: DecorationImage(
              image: NetworkImage(
                  "${item.thumbnail}"),
              fit: BoxFit.cover),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadiusDirectional.circular(12),
          ),
        ),
        child: Stack(
          children: <Widget>[
            Positioned(
              top: px(15),
              right: px(16),
              child: Container(
                width: px(94),
                height: px(38),
                decoration: BoxDecoration(
                  border: Border.all(width: px(1), color: Colors.white),
                  borderRadius: BorderRadius.all(
                    Radius.circular(
                      px(19),
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Image.asset(
                      'assets/images/icon/whitestar.png',
                      width: px(26),
                      height: px(25),
                    ),
                    Container(
                        child: Text(
                      '付费',
                      style: TextStyle(
                        fontSize: px(22),
                        color: Colors.white,
                      ),
                    )),
                  ],
                ),
              ),
            ),
            Positioned(
              bottom: px(15),
              left: px(10),
              child: Container(
                width: px(94),
                height: px(38),
                decoration: BoxDecoration(
                  color: Color.fromRGBO(255, 255, 255, 0.8),
                  border: Border.all(width: px(1), color: Colors.white),
                  borderRadius: BorderRadius.all(
                    Radius.circular(
                      px(19),
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Image.asset(
                      'assets/images/icon/heart_red.png',
                      width: px(20),
                      height: px(19),
                    ),
                    Container(
                        child: Text(
                      '${item.watch}',
                      style: TextStyle(
                        fontSize: px(22),
                        color: Colors.red,
                      ),
                    )),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  LovevideoBloc lovevideoBloc = LovevideoBloc();
  BuyvideoBloc buyvideoBloc = BuyvideoBloc();

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: tabs.length,
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(kToolbarHeight),
          child: AppBar(
            title: Container(
              color: Colors.white,
              child: SafeArea(
                child: Column(
                  children: <Widget>[
                    getTabBar(),
                  ],
                ),
              ),
            ),
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            BlocProvider(
              bloc: lovevideoBloc,
              child: getTabview(context, lovevideoBloc),
            ),
            BlocProvider(
              bloc: buyvideoBloc,
              child: getBuyTabview(context, buyvideoBloc),
            ),
          ],
        ),
      ),
    );
  }
}
